package com.rockbb.common.utilities.i18n;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.ObjectMapper;

import com.rockbb.common.utilities.text.JacksonMapper;

public class LanguageUtil {

	public static Map<String, String> extractLocales(String value, List<Language> languages) {
		Map<String,String> locales = new HashMap<String,String>();

		ObjectMapper mapper = JacksonMapper.getInstance();
		try {
			locales = mapper.readValue(value, Map.class);
		} catch (JsonParseException e) {
			// Do nothing
		} catch (IOException e) {
			// Do nothing
		}

		for(int i=0; i<languages.size(); i++) {
			String content = locales.get(languages.get(i).getFullName());
			if (content == null) {
				locales.put(languages.get(i).getFullName(), "");
			}
		}
		return locales;
	}

	public static String compressLocales(Map<String, String> locales) {
		String value = "";
		ObjectMapper mapper = JacksonMapper.getInstance();
		try {
			value = mapper.writeValueAsString(locales);
		} catch (JsonGenerationException e) {
			// Do nothing
		} catch (IOException e) {
			// Do nothing
		}
		return value;
	}
}
